package com.yuhanginfo.modules.web.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.WebClassService;
import com.yuhanginfo.modules.web.service.WebYongUserService;

/**
 * 企业信息
 *
 */

@Controller
@RequestMapping(value = "${frontPath}/companyInfo")
public class CompanyInfoController extends BaseController{
	@Autowired
	private WebClassService webClassService;
	@Autowired
	private WebYongUserService webYongUserService;
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"", ""})
	public String getInfo(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		WebYongUser yongUser=WebUserUtils.getYongUser();
		yongUser=webYongUserService.get(yongUser.getId());
		//WebYongUser yongUser=webYongUserService.get("6");//普通用户
		//WebYongUser yongUser=customerService.get("8");//会员
		if(yongUser.getId()!=null) {
			model.addAttribute("yongUser", yongUser);
			model.addAttribute("actionCenter", "companyInfo");
			return "modules/web/companyInfo";
		}else {
			return "modules/web/webLogin";//到登陆页面
		}
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"update"})
	public String update(HttpServletRequest request, HttpServletResponse response, Model model,WebYongUser user) {
		webYongUserService.updateLinkMan(user);
		return "redirect:" + frontPath + "/companyInfo";
	}
	
	@ResponseBody
	@RequiresPermissions("user")
	@RequestMapping(value = "checkEmail",method = RequestMethod.POST)
	public boolean checkEmail(String email,String oldEmail) {
		WebYongUser user=new WebYongUser();
		user.setEmail(email);
		if (email!=null && email.equals(oldEmail)) {
			return true;
		} else if (email!=null && !webYongUserService.existUserByEmail(user)) {
			return true;
		}
		return false;
	}
	
	@ResponseBody
	@RequiresPermissions("user")
	@RequestMapping(value = "checkLinkmanMobile",method = RequestMethod.POST)
	public boolean checkLinkmanMobile(String linkmanPhone,String oldLinkmanPhone) {
		WebYongUser user=new WebYongUser();
		user.setLinkmanPhone(linkmanPhone);
		if (linkmanPhone!=null && linkmanPhone.equals(oldLinkmanPhone)) {
			return true;
		} else if (linkmanPhone!=null && !webYongUserService.existUserByLinkmanMobile(user)) {
			return true;
		}
		return false;
	}
}
